/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upn.sigemfi.can.bc;

import edu.upn.sigemfi.can.be.Pais;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Jhosep
 */
public class PaisDelegate {

    private PaisFacadeLocal local;

    public PaisDelegate() {
        local = lookupPaisFacade();
    }

    public void crear(Pais pais) throws Exception {
        try {
            local.create(pais);
        } catch (Exception e) {
            local = lookupPaisFacade();
            throw new Exception("Crear Pais" + e.getMessage(), e);
        }
    }

    public void editar(Pais pais) throws Exception {
        try {
            local.edit(pais);
        } catch (Exception e) {
            local = lookupPaisFacade();
            throw new Exception("Editar Pais" + e.getMessage(), e);
        }
    }

    public void eliminar(Pais pais) throws Exception {
        try {
            local.remove(pais);
        } catch (Exception e) {
            local = lookupPaisFacade();
            throw new Exception("Eliminar Pais" + e.getMessage(), e);
        }
    }

    public Pais buscar(long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupPaisFacade();
            throw new Exception("Buscar Pais" + e.getMessage(), e);
        }
    }

    public List<Pais> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupPaisFacade();
            throw new Exception("Listar Pais. " + e.getMessage(), e);
        }
    }

    private PaisFacadeLocal lookupPaisFacade() {
        try {
            Context c = new InitialContext();
            return (PaisFacadeLocal) c.lookup("java:comp/env/PaisFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
